package com.jason.jpa.web;

import com.jason.jpa.dao.UserRepository;
import com.jason.jpa.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Jason on 2018-04-18.
 */
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserRepository userRepository;

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public String save(@ModelAttribute User user) {
        userRepository.save(user);
        return "sucdess";
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public List<User> getAll() {
        return userRepository.findAll();
    }

    @RequestMapping(value = "/{page}/{size}", method = RequestMethod.GET)
    public Page<User> getAllByPage(@PathVariable Integer page, @PathVariable Integer size) {
        Pageable pageable = new PageRequest(page, size);
        return userRepository.findAll(pageable);
    }
}
